

package com.xgit.iov.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgit.iov.admin.service.SysRoleDataRuleService;
import com.xgit.iov.admin.service.SysRoleMenuService;
import com.xgit.iov.admin.service.SysRoleService;
import com.xgit.iov.common.core.entity.SysRole;
import com.xgit.iov.common.core.model.RoleVO;
import com.xgit.iov.common.core.util.R;
import com.xgit.iov.common.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lengleng
 * @date 2019/2/1
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理模块")
public class RoleController {

	private final SysRoleService sysRoleService;

	private final SysRoleMenuService sysRoleMenuService;

	private final SysRoleDataRuleService sysRoleDataRuleService;

	/**
	 * 通过ID查询角色信息
	 *
	 * @param id ID
	 * @return 角色信息
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "通过ID查询角色信息")
	public R getById(@PathVariable Long id) {
		return R.ok(sysRoleService.getById(id));
	}

	/**
	 * 添加角色
	 *
	 * @param sysRole 角色信息
	 * @return success、false
	 */
	@LogAnnotation("添加角色")
	@ApiOperation(value = "添加角色")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	public R save(@Valid @RequestBody SysRole sysRole) {
		return R.ok(sysRoleService.save(sysRole));
	}

	/**
	 * 修改角色
	 *
	 * @param sysRole 角色信息
	 * @return success/false
	 */

	@LogAnnotation("修改角色")
	@ApiOperation(value = "修改角色")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	public R update(@Valid @RequestBody SysRole sysRole) {
		return R.ok(sysRoleService.updateById(sysRole));
	}

	/**
	 * 删除角色
	 *
	 * @param ids
	 * @return
	 */
	@LogAnnotation("删除角色")
	@ApiOperation(value = "删除角色")
	@DeleteMapping("/{ids}")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	public R removeById(@PathVariable Long[] ids) {
		return R.ok(sysRoleService.removeRoleById(ids));
	}

	/**
	 * 获取角色列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/list")
	@ApiOperation(value = "获取角色列表")
	public R listRoles(Long tenantId) {
		return R.ok(sysRoleService.listRoles(tenantId));
	}

	/**
	 * 分页查询角色信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页查询角色信息")
	public R getRolePage(Page page, SysRole sysRole) {
		return R.ok(sysRoleService.page(page, Wrappers.query(sysRole)));
	}

	/**
	 * 更新角色菜单
	 *
	 * @param roleVo 角色对象
	 * @return success、false
	 */
	@LogAnnotation("更新角色菜单")
	@ApiOperation(value = "更新角色菜单")
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public R saveRoleMenus(@RequestBody RoleVO roleVo) {
		SysRole sysRole = sysRoleService.getById(roleVo.getRoleId());
		return R.ok(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode()
			, roleVo.getRoleId(), roleVo.getMenuIds()));
	}

	/**
	 * 更新数据权限菜单
	 *
	 * @param roleVo 角色对象
	 * @return success、false
	 */
	@LogAnnotation("更新数据权限菜单")
	@ApiOperation(value = "更新数据权限菜单")
	@PutMapping("/dataRule")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public R saveRoleDataRule(@RequestBody RoleVO roleVo) {
		SysRole sysRole = sysRoleService.getById(roleVo.getRoleId());
		return R.ok(sysRoleDataRuleService.saveRoleDataRule(sysRole.getRoleCode()
			, roleVo.getRoleId(), roleVo.getDataRuleIds()));
	}
}
